package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.Service;
import com.jlwl.houseservice.service.ServiceService;
import com.jlwl.houseservice.vo.ServiceDetailVO;
import com.jlwl.houseservice.vo.ServiceQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务控制器
 */
@RestController
@RequestMapping("/services")
public class ServiceController {

    @Autowired
    private ServiceService serviceService;

    /**
     * 分页查询服务列表
     */
    @GetMapping
    public Result<Page<Service>> list(ServiceQueryVO queryVO) {
        Page<Service> page = serviceService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取服务详情
     */
    @GetMapping("/{id}")
    public Result<ServiceDetailVO> getDetail(@PathVariable Integer id) {
        ServiceDetailVO detailVO = serviceService.getServiceDetail(id);
        if (detailVO == null) {
            return Result.error("服务不存在");
        }
        return Result.success(detailVO);
    }

    /**
     * 获取特色服务列表
     */
    @GetMapping("/special")
    public Result<List<Service>> getSpecialServices() {
        List<Service> list = serviceService.getSpecialServices();
        return Result.success(list);
    }

    /**
     * 根据分类获取服务列表
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<Service>> getServicesByCategory(@PathVariable Integer categoryId) {
        List<Service> list = serviceService.getServicesByCategory(categoryId);
        return Result.success(list);
    }

    /**
     * 添加服务
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Service> add(@RequestBody Service service) {
        service.setCreatedAt(LocalDateTime.now());
        service.setUpdatedAt(LocalDateTime.now());
        serviceService.save(service);
        return Result.success(service);
    }

    /**
     * 更新服务
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> update(@PathVariable Integer id, @RequestBody Service service) {
        Service existService = serviceService.getById(id);
        if (existService == null) {
            return Result.error("服务不存在");
        }
        
        service.setId(id);
        service.setUpdatedAt(LocalDateTime.now());
        serviceService.updateById(service);
        return Result.success();
    }

    /**
     * 删除服务
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> delete(@PathVariable Integer id) {
        Service existService = serviceService.getById(id);
        if (existService == null) {
            return Result.error("服务不存在");
        }
        
        serviceService.removeById(id);
        return Result.success();
    }

    /**
     * 更新服务状态
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        Service existService = serviceService.getById(id);
        if (existService == null) {
            return Result.error("服务不存在");
        }
        
        Service updateService = new Service();
        updateService.setId(id);
        updateService.setStatus(status);
        updateService.setUpdatedAt(LocalDateTime.now());
        serviceService.updateById(updateService);
        return Result.success();
    }
} 